package me.xiaoyan.ehr.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.xiaoyan.ehr.po.CheckRecord;
import me.xiaoyan.ehr.po.Employee;
import me.xiaoyan.ehr.po.EmployeeInfo;
import me.xiaoyan.ehr.service.CheckRecordService;
import me.xiaoyan.ehr.service.EmployeeInfoService;
import me.xiaoyan.ehr.service.EmployeeService;
import me.xiaoyan.ehr.vo.ApiResponseData;
import me.xiaoyan.ehr.vo.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/emp")
@ApiOperation(
        value = "员工管理"
)
public class EmployeeController extends BaseController{
    private EmployeeService employeeService;
    private EmployeeInfoService employeeInfoService;
    private CheckRecordService checkRecordService;

    @Autowired
    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }


    @Autowired
    public void setEmployeeInfoService(EmployeeInfoService employeeInfoService) {
        this.employeeInfoService = employeeInfoService;
    }

    @Autowired
    public void setCheckRecordService(CheckRecordService checkRecordService) {
        this.checkRecordService = checkRecordService;
    }

    @ApiOperation(
            value = "查询员工列表", httpMethod = "GET", notes = "查询员工列表"
    )
    @GetMapping("/list")
    public ApiResponseData<IPage<Employee>> getList(@RequestParam Map<String, String> params) {
        ApiResponseData<IPage<Employee>> data = new ApiResponseData<>();
        IPage<Employee> employeeIPage = employeeService.selectByParams(params);
        data.setData(employeeIPage);
        return data;
    }

    @GetMapping("/check_emp_id")
    public ApiResponseData checkEmpIdExists(String empId) {
        ApiResponseData data = new ApiResponseData();
        if (employeeService.getByEmpId(empId) != null) {
            return data.createError(4, "员工编号已经存在");
        }
        return data;
    }

    @GetMapping("/check_id_card_number")
    public ApiResponseData checkIdCardBNumberExists(String idCardNumber) {
        ApiResponseData data = new ApiResponseData();
        if (employeeService.getByIdCardNumber(idCardNumber) != null) {
            return data.createError(5, "身份证号码已经存在");
        }
        return data;
    }
    @ApiOperation(
            value = "新增员工信息", httpMethod = "POST", notes = "新增员工信息"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "employee", value = "员工json对象", dataType = "Employee")
    })
    @PostMapping("/save")
    public ApiResponseData save(@RequestBody Employee employee) {
        ApiResponseData data = new ApiResponseData();
        if (!StringUtils.hasLength(employee.getEmpId())) {
            return data.createError(1, "员工编号不允许为空");
        }
        if (!StringUtils.hasLength(employee.getIdCardNumber())) {
            return data.createError(2, "身份号码不允许为空");
        }
        try {
            if (employee.getId() > 0) { // 修改员工信息
                employeeService.update(employee);
            } else { // 新增员工
                employeeService.create(employee);
            }
        } catch (BizException e) {
            return data.createError(3, e.getMessage());
        }
        return data;
    }
    @ApiOperation(
            value = "更新员工基本信息", httpMethod = "POST"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "employeeInfo", value = "员工基本信息json对象", dataType = "EmployeeInfo")
    })
    @PostMapping("/save-detail")
    public ApiResponseData saveDetail(@RequestBody EmployeeInfo employeeInfo){
        ApiResponseData data = new ApiResponseData();
        employeeInfoService.updateById(employeeInfo);
        return data;
    }

    @ApiOperation(
            value = "更新员工基本信息", httpMethod = "POST"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "employeeInfo", value = "员工基本信息json对象", dataType = "EmployeeInfo")
    })
    @PostMapping("/update-info")
    public ApiResponseData update(@RequestBody Employee employee){
        employee.setId(getLoginUser().getId());
        ApiResponseData data = new ApiResponseData();
        employeeService.updateById(employee);
        return data;
    }

    @Value("${application.upload.dir}")
    private String uploadDir;
    @ApiOperation(
            value = "获取员工基本信息", httpMethod = "GET"
    )
    @GetMapping("/get-info")
    public ApiResponseData<Employee> empInfo(HttpServletRequest request) {


        ApiResponseData<Employee> data = new ApiResponseData();
        data.setData(this.getLoginUser().getEmployee());
        return data;
    }
    @ApiOperation(
            value = "获取员工基本信息", httpMethod = "GET"
    )
    @GetMapping("/get-detail")
    public ApiResponseData<EmployeeInfo> empDetailInfo(){
        // TODO 先固定为1
        int id = 1;
        ApiResponseData<EmployeeInfo> data = new ApiResponseData();
        EmployeeInfo employeeInfo = employeeInfoService.getById(id);
        data.setData(employeeInfo);
        return data;
    }
    @ApiOperation(
            value = "获取今日打卡记录", httpMethod = "GET"
    )
    @GetMapping("/record-today")
    public ApiResponseData<List<CheckRecord>> todayRecord(){
        ApiResponseData<List<CheckRecord>> data = new ApiResponseData();
        data.setData(checkRecordService.getTodayRecord());
        return data;
    }
}
